package com.itheima52.mobilesafe.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.telephony.SmsManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

/*
 * 监听手机开机的广播
 */
public class BootCompleteReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		SharedPreferences sp = context.getSharedPreferences("config",
				context.MODE_PRIVATE);
		String sim = sp.getString("sim", null);// 获取绑定的sim卡号
		boolean protect = sp.getBoolean("protect", false);
		// 只有防盗保护开启的前提下,才进行sim卡的判断
		if (protect) {
			if (!TextUtils.isEmpty(sim)) {
				// 获取当前手机的sim卡
				TelephonyManager tm = (TelephonyManager) context
						.getSystemService(context.TELEPHONY_SERVICE);
				String currentSim = tm.getSimSerialNumber()+"11";
				if (sim.equals(currentSim)) {
					System.out.println("手机安全");
				} else {
					System.out.println("sim卡变化,发送报警短信.");
					String phone = sp.getString("safe_phone", "");// 读取安全号码
					SmsManager smsManager = SmsManager.getDefault();
					// 目标电话号码,服务中心,文本内容,发送成功的intent,接收成功的intent
					smsManager.sendTextMessage(phone, null,
							"sim card changed!", null, null);
					
				}

			}
		}

	}

}
